﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class MapTest : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string latitudeStr = "";
                string longitudeStr = "";

                // Coloca mapa e alfinete na pagina com posicionamento do servidor.

                if ((Request.QueryString != null) && (Request.QueryString.Count > 0))
                {
                    latitudeStr = Request.QueryString["latitude"].ToString();
                    longitudeStr = Request.QueryString["longitude"].ToString();
                    BuildScript(latitudeStr, longitudeStr);
                }
                //else
                //{
                //    latitudeStr = "-20";// Request.QueryString["latitude"].ToString();
                //    longitudeStr = "-40";// Request.QueryString["longitude"].ToString();
                //}

                //BuildScript(latitudeStr, longitudeStr);

                if (!IsPostBack)
                {
                    string conStr = @"Data Source=www.lifeware.com.br\sqlexpress,2272;Initial Catalog=MERCATOR_hist;Persist Security Info=True;User ID=sa;Password=life2011";
                        //WebConfig.GetDBConn();

                    DataSet deviceDataSet = getData(conStr, "Device", "");
                    System.Data.DataTable deviceDT = deviceDataSet.Tables[0];

                    String Locations = "";
                    string Latitude = "";
                    string Longitude = "";
                    foreach (DataRow r in deviceDT.Rows)
                    {
                        // bypass empty rows	 	
                        if (r["LastLatitude"].ToString().Trim().Length == 0)
                            continue;

                        Latitude = r["LastLatitude"].ToString();
                        Longitude = r["LastLongitude"].ToString();

                        Locations += BuildScript(Latitude.Replace(",", "."), Longitude.Replace(",", "."));
                    }

                    // construct the final script
                    //alert(window.location.host);
                    js.Text = @"<script type='text/javascript'>
                            function initialize() {
                              if (GBrowserIsCompatible()) {

                                var map = new GMap2(document.getElementById('map_canvas'));
                                map.setCenter(new GLatLng(" + Latitude + "," + Longitude + @"),15); 
                                " + Locations + @"
                                map.setUIToDefault();
                              }
                            }
                            </script> ";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now + " - Erro : " + ex.Message + "\nStackTrace:" + ex.StackTrace);
                //throw;
            }

            //}

        }


        private string BuildScript(string Latitude, string Longitude)
        {
            String Locations = "";

            // create a line of JavaScript for marker on map for this record	
            Locations = Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
            //}

            // construct the final script
            //alert(window.location.host);
            //            js.Text = @"<script type='text/javascript'>
            //                            function initialize() {
            //                              if (GBrowserIsCompatible()) {
            //
            //                                var map = new GMap2(document.getElementById('map_canvas'));
            //                                map.setCenter(new GLatLng(" + Latitude + "," + Longitude + @"),15); 
            //                                " + Locations + @"
            //                                map.setUIToDefault();
            //                              }
            //                            }
            //                            </script> ";

            return Locations;
        }

        public System.Data.DataSet getData(string connectionString, string currentTableName, string whereClause)
        {
            System.Data.DataSet tempDataSet = new System.Data.DataSet();
            SqlDataAdapter currentDataAdapter = null;

            try
            {
                SqlConnection dataConnection = new SqlConnection(connectionString);

                //OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder();//(OleDbDataAdapter)currentDataAdapter);
                //cmdBuilder.GetInsertCommand();

                string SQL = "SELECT * FROM " + currentTableName;
                if ((whereClause != null) && (whereClause.Trim() != ""))
                    SQL += " WHERE " + whereClause;

                SqlCommand tableCommand = new SqlCommand(SQL, dataConnection);

                currentDataAdapter = new SqlDataAdapter(tableCommand);

                currentDataAdapter.Fill(tempDataSet);
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now + " - Erro : " + ex.Message + "\nStackTrace:" + ex.StackTrace);
                //throw;
            }

            return tempDataSet;
        }
    }
}